import Common from "../common/Common";
import Define from "../common/Define";
import ResInfo from "../common/ResInfo";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html



export default class ResManager{

    private static prefabArr:ResInfo[] = [];

    public static loadPrefab(url:string,callBack:Function = null){
        console.log("loadPrefab url = " + url);
        let obj = this.isAlreadyExsit(Define.loadTypePrefab,url);
        if(obj == null){
            cc.loader.loadRes(url, cc.Prefab, function(err, prefab) {
                if (err) {
                    cc.log(err.message || err);
                    return;
                }
                if(callBack != null){
                    callBack(prefab);
                }

                this.prefabArr.push(new ResInfo(url,prefab));
                
            }.bind(this));
        }else{
            if(callBack != null){
                callBack(obj);
            }
        }
    }

    private static isAlreadyExsit(loadType:number,ulr:string):cc.Object{
        let arr = null;
        if(loadType == Define.loadTypePrefab){
            arr = this.prefabArr;
        }

        for (let index = 0; index < arr.length; index++) {
            if(arr[index].key == ulr){
                return arr[index].node;
            }
        }
        return null;
    }

    public static loadSound(soundId:number,callBack:Function){
        //加载声音
        cc.loader.loadRes("sounds/music_christmas", cc.AudioClip,function(err, res){
            if(err) {
                console.log("【音频】资源加载错误");
                return ;
            }
            console.log("【音频】资源加载完成");
            callBack(res)
        });
    }
}

/*
cc.loader.loadResDir(url, cc.AudioClip,function(err, res){
    if(err) {
        cc.error("【音频】资源加载错误");
        return ;
    }
});
--------------------- 
作者：年少轻狂s 
来源：CSDN 
原文：https://blog.csdn.net/nsqks/article/details/79899972 
版权声明：本文为博主原创文章，转载请附上博文链接！
*/
/*
let self = this;
cc.loader.loadRes(url, cc.SpriteFrame, function(err, spFrame) {
    if (err) {
        cc.log(err.message || err);
        return;
    }
    let node = new cc.Node('newNode');
    const sprite = node.addComponent(cc.Sprite);
    sprite.spriteFrame = spFrame;
    self.node.addChild(node);
});
let self = this;
cc.loader.loadRes(url, cc.Prefab, function(err, prefab) {
    if (err) {
        cc.log(err.message || err);
        return;
    }
    let node = cc.instantiate(prefab);
    node.active = true;
    self.node.addChild(node);
});

let self = this;
cc.loader.loadRes(url, cc.AnimationClip, function(err, clip) {
    if (err) {
        cc.log(err.message || err);
        return;
    }
    let node = new cc.Node('animNode');
    let animCtrl = node.addComponent(cc.Animation);
    animCtrl.addClip(clip);
    animCtrl.play('run');
    self.node.addChild(node);
});

---------------------
let self = this;
cc.loader.loadRes(url, sp.SkeletonData, function(err, spine) {
    if (err) {
        cc.log(err.message || err);
        return;
    }
    let node = new cc.Node('spineNode');
    const ske = node.addComponent(sp.Skeleton);
    ske.skeletonData = spine;
    ske.setAnimation(0, 'run', false);
    self.node.addChild(node);
});


*/